{% extends "base.html" %} {% block title %}健康知识 - 运动健康管理系统{%
endblock %} {% block content %}
<div class="container py-5">
  <div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="mb-0"><i class="bi bi-book"></i> 健康知识</h1>
  </div>

  <!-- 搜索和分类筛选 -->
  <div class="row mb-4">
    <div class="col-md-6">
      <form
        action="{{ url_for('search_knowledge') }}"
        method="GET"
        class="d-flex">
        <input
          type="text"
          name="keyword"
          class="form-control me-2"
          placeholder="搜索健康知识..."
          value="{{ keyword if keyword }}" />
        <button type="submit" class="btn btn-primary">
          <i class="bi bi-search"></i> 搜索
        </button>
      </form>
    </div>
    <div class="col-md-6">
      <div class="d-flex justify-content-end">
        <div class="btn-group" role="group">
          <a
            href="{{ url_for('health_knowledge') }}"
            class="btn btn-outline-primary {% if not category %}active{% endif %}"
            >全部</a
          >
          <a
            href="{{ url_for('health_knowledge', category='饮食健康') }}"
            class="btn btn-outline-primary {% if category == '饮食健康' %}active{% endif %}"
            >饮食健康</a
          >
          <a
            href="{{ url_for('health_knowledge', category='运动健身') }}"
            class="btn btn-outline-primary {% if category == '运动健身' %}active{% endif %}"
            >运动健身</a
          >
          <a
            href="{{ url_for('health_knowledge', category='心理健康') }}"
            class="btn btn-outline-primary {% if category == '心理健康' %}active{% endif %}"
            >心理健康</a
          >
          <a
            href="{{ url_for('health_knowledge', category='疾病预防') }}"
            class="btn btn-outline-primary {% if category == '疾病预防' %}active{% endif %}"
            >疾病预防</a
          >
          <a
            href="{{ url_for('health_knowledge', category='生活方式') }}"
            class="btn btn-outline-primary {% if category == '生活方式' %}active{% endif %}"
            >生活方式</a
          >
          <a
            href="{{ url_for('health_knowledge', category='其他') }}"
            class="btn btn-outline-primary {% if category == '其他' %}active{% endif %}"
            >其他</a
          >
        </div>
      </div>
    </div>
  </div>

  {% if articles %}
  <div class="row">
    {% for article in articles %}
    <div class="col-md-6 mb-4">
      <div class="card h-100 shadow-sm">
        <div class="card-header bg-light">
          <span class="badge bg-secondary">{{ article.category }}</span>
        </div>
        <div class="card-body">
          <h5 class="card-title">{{ article.title }}</h5>
          <p class="card-text text-muted">
            <small
              >发布于: {{ article.created_at.strftime('%Y-%m-%d') }} | 浏览: {{
              article.view_count }}</small
            >
          </p>
        </div>
        <div class="card-footer bg-transparent">
          <a
            href="{{ url_for('article_detail', article_id=article.id) }}"
            class="btn btn-outline-primary w-100"
            >阅读全文</a
          >
        </div>
      </div>
    </div>
    {% endfor %}
  </div>

  <!-- 分页 -->
  {% if pagination %}
  <nav aria-label="Page navigation" class="mt-4">
    <ul class="pagination justify-content-center">
      <li class="page-item {% if pagination.page == 1 %}disabled{% endif %}">
        <a
          class="page-link"
          href="{{ url_for('health_knowledge', page=pagination.page-1, category=category, keyword=keyword) }}"
          >上一页</a
        >
      </li>
      {% for p in range(1, pagination.pages + 1) %}
      <li class="page-item {% if p == pagination.page %}active{% endif %}">
        <a
          class="page-link"
          href="{{ url_for('health_knowledge', page=p, category=category, keyword=keyword) }}"
          >{{ p }}</a
        >
      </li>
      {% endfor %}
      <li
        class="page-item {% if pagination.page == pagination.pages %}disabled{% endif %}">
        <a
          class="page-link"
          href="{{ url_for('health_knowledge', page=pagination.page+1, category=category, keyword=keyword) }}"
          >下一页</a
        >
      </li>
    </ul>
  </nav>
  {% endif %} {% else %}
  <div class="alert alert-info">
    {% if keyword %}
    <p class="mb-0">没有找到与"{{ keyword }}"相关的健康知识。</p>
    {% elif category %}
    <p class="mb-0">暂无"{{ category }}"分类的健康知识文章。</p>
    {% else %}
    <p class="mb-0">暂无健康知识文章。</p>
    {% endif %}
  </div>
  {% endif %}
</div>
{% endblock %}
